<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>promise</title>
</head>
<body>
	<input type="button" value="读取文件" onclick="read()">
	<div name="result" id="result" style="white-space: pre;">
		
	</div>
	<script>
		function CreateXMLHTTP() {
			if(window.ActiveXObject){
				var objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} else {
				if(window.XMLHttpRequest) {
					var objXmlHttp = new XMLHttpRequest();
				} else {
					alert("error");
				}
			}
			return objXmlHttp;
		}
		function getData(fileName) {
			return new Promise(function(resolve,reject) {
				var objXmlHttp = CreateXMLHTTP();
				objXmlHttp.open("GET",fileName,true);
				objXmlHttp.onreadychange = function() {
					if(objXmlHttp.readyState == 4) {
						if(objXmlHttp.status == 200) {
							resolve(objXmlHttp.responseText);
						} else {
							reject();
						}
					}
				}
				objXmlHttp.send();
			})
		}
		function read() {
			Promise.all([getData("1.text"),getData("2.text"),getData("3.text")]).then(function(responses) {
				var result = document.getElementById("result");
				responses.innerHTML += responses + "<br/>";
			},function() {
				alert("读取文件失败")
			})
		}
	</script>
</body>
</html>